/**
 * Generali Solutions d'assurances - Tous droits reserves &copy; 2007 - 2010
 */
package fr.generali.accueilclient.commun.exception;

import java.net.SocketTimeoutException;

import javax.xml.ws.WebServiceException;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;

/**
 * The Class AbstractAccueilClientAnalyseWSException.
 * 
 * @author Holmes Kelly
 */
public abstract class AbstractAccueilClientAnalyseWSException {

    /**
     * Instantiates a new abstract accueil client analyse ws exception.
     */
    protected AbstractAccueilClientAnalyseWSException() {
        // Empty constructor
    }

    /**
     * Analyze ws exception.
     * 
     * @param e the web service exception
     * @throws AccueilClientTechnicalException the accueil client technical
     *             exception
     */
    protected void analyzeWsException(WebServiceException e) throws AccueilClientTechnicalException {
        if (ExceptionUtils.indexOfThrowable(e, SocketTimeoutException.class) != -1) {
            // receiveTimeout = Read timed out
            // connectionTimeout = connect timed out
            String stackTrace = ExceptionUtils.getFullStackTrace(e);
            if (StringUtils.indexOf(stackTrace, "Read timed out") != -1) {
                throw new AccueilClientTechnicalException("Socket timed out  : Read timed out", e);
            } else if (StringUtils.indexOf(stackTrace, "connect timed out") != -1) {
                throw new AccueilClientTechnicalException("Socket timed out : connect timed out", e);
            } else if (StringUtils.indexOf(stackTrace, "Connection timed out") != -1) {
                throw new AccueilClientTechnicalException("Socket timed out : connect timed out", e);
            } else {
                throw new AccueilClientTechnicalException("SOCKET TIMED OUT", e);
            }
        } else
            throw new AccueilClientTechnicalException("Cannot call server", e);
    }
}
